/*
 * @Description: Vue配置文件
 * @Author: qiyu & seadon
 * @LastEditors: qiyu qiyu@byteluck.com
 * @Date: 2021-03-09 18:21:35
 * @LastEditTime: 2023-03-02 18:54:57
 */
const path = require('path')
const webpack = require('webpack')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const { setGitVersionToMeta } = require('@byteluck/git-info-to-meta')
const devServer = require('./config/proxy.ts')
const {
	desktopCommonCDN,
	mobileCommonCDN,
	desktopAssetsCDN,
	mobileAssetsCDN,
	externalsCDN,
	cdnObject,
	generateCDNPathList,
	getTheme,
	loginCommonCDN,
	ddloginCommonCDN,
	authorizationCommonCDN,
} = require('./config/cdn.ts')
const cdnPath = process.env.VUE_APP_CDN_PATH

const isProduction = process.env.NODE_ENV === 'production'
module.exports = {
	// runtimeCompiler: true,
	publicPath: process.env.VUE_APP_PUBLIC_PATH,
	lintOnSave: false,
	pages: {
		index: 'src/entry_index/main.ts',
		desktop: 'src/entry_desktop/main.ts',
		mobile: 'src/entry_mobile/main.ts',
		login: 'src/entry_login/main.ts',
		authorization: 'src/entry_authorization/main.ts',
		zyddlogin: 'src/entry_zydd/main.ts',
		ddlogin: 'src/entry_dd/main.ts',
	},
	pluginOptions: {
		// 第三方插件配置
		'style-resources-loader': {
			preProcessor: 'less',
			patterns: [
				path.resolve(__dirname, './src/assets/styles/variable.less'),
				path.resolve(__dirname, './src/assets/styles/mobile_variable.less'),
			], // less所在文件路径
		},
	},
	// 生产环境source map，上线要关闭，内部环境可临时开启
	productionSourceMap: !isProduction,
	configureWebpack: config => {
		if (!isProduction) {
			config.devtool = 'inline-source-map'
		} else {
			config.devtool = false
			config.externals = externalsCDN.externals
			// config.devtool = 'source-map'
		}
		config.plugins.push(
			new webpack.IgnorePlugin({
				resourceRegExp: /^\.\/locale$/,
				contextRegExp: /moment$/,
			})
		)
		config.plugins.push(
			new MiniCssExtractPlugin({
				filename: '[name].[hash].css',
			})
		)
	},
	chainWebpack(config) {
		const svgRule = config.module.rule('svg')
		svgRule.set('type', 'asset/resource')

		// cdn资源整理
		config.plugin('html-desktop').tap(options => {
			options[0].cdn = isProduction ? desktopAssetsCDN : desktopCommonCDN
			options[0].okI18n = generateCDNPathList(
				cdnPath,
				cdnObject,
				['ok-i18n'],
				'js'
			)[0]
			options[0].cdnJSON = JSON.stringify(cdnObject)
			options[0].getTheme = getTheme.toString()
			options[0].generateCDNPathList = generateCDNPathList.toString()
			return options
		})
		// 移动端CDN资源整理
		config.plugin('html-mobile').tap(options => {
			options[0].title = process.env.VUE_APP_TITLE
			options[0].cdn = isProduction ? mobileAssetsCDN : mobileCommonCDN
			options[0].okI18n = generateCDNPathList(
				cdnPath,
				cdnObject,
				['ok-i18n'],
				'js'
			)[0]
			options[0].cdnJSON = JSON.stringify(cdnObject)
			options[0].getTheme = getTheme.toString()
			options[0].generateCDNPathList = generateCDNPathList.toString()
			return options
		})
		//登录页面CDN资源整理
		config.plugin('html-login').tap(options => {
			options[0].cdn = loginCommonCDN
			options[0].getTheme = getTheme.toString()
			options[0].cdnJSON = JSON.stringify(cdnObject)
			options[0].generateCDNPathList = generateCDNPathList.toString()
			return options
		})
		// ddlogin页面CDN资源整理
		config.plugin('html-ddlogin').tap(options => {
			options[0].cdn = ddloginCommonCDN
			options[0].getTheme = getTheme.toString()
			options[0].cdnJSON = JSON.stringify(cdnObject)
			options[0].okI18n = generateCDNPathList(
				cdnPath,
				cdnObject,
				['ok-i18n'],
				'js'
			)[0]
			options[0].generateCDNPathList = generateCDNPathList.toString()
			return options
		})
		// zyddlogin页面CDN资源整理
		config.plugin('html-zyddlogin').tap(options => {
			options[0].cdn = ddloginCommonCDN
			options[0].getTheme = getTheme.toString()
			options[0].cdnJSON = JSON.stringify(cdnObject)
			options[0].okI18n = generateCDNPathList(
				cdnPath,
				cdnObject,
				['ok-i18n'],
				'js'
			)[0]
			options[0].generateCDNPathList = generateCDNPathList.toString()
			return options
		})
		// authorization页面CDN资源整理
		config.plugin('html-authorization').tap(options => {
			options[0].cdn = authorizationCommonCDN
			options[0].getTheme = getTheme.toString()
			options[0].cdnJSON = JSON.stringify(cdnObject)
			options[0].generateCDNPathList = generateCDNPathList.toString()
			return options
		})

		config.resolve.alias.set('@', path.resolve(__dirname, 'src'))
		config.output.devtoolModuleFilenameTemplate(info =>
			info.resourcePath.match(/\.vue$/) && !info.identifier.match(/type=script/)
				? `webpack-generated:///${info.resourcePath}?${info.hash}`
				: `webpack-vue:///${info.resourcePath}`
		)
		config.module
			.rule('vue')
			.use('vue-loader')
			.tap(options => {
				options.compilerOptions = {
					...(options.compilerOptions || {}),
					isCustomElement: tag => /^ok-/.test(tag) || /^wfc-/.test(tag),
				}
				return options
			})

		config.optimization.minimizer('terser').tap(args => {
			args[0].terserOptions.compress.drop_console = true
			return args
		})

		setGitVersionToMeta(config, ['desktop', 'mobile'])

		process.env.npm_config_report &&
			config
				.plugin('webpack-bundle-analyzer')
				.use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin)
	},
	devServer,
}
